package com.xiaoshuidi.cloud.module.member.service.securitycheckprogram;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram.vo.Gridders;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram.vo.SecurityCheckProgramGriddersCreateReqVO;
import com.xiaoshuidi.cloud.module.member.convert.securitycheckprogram.SecurityCheckProgramGriddersConvert;
import com.xiaoshuidi.cloud.module.member.mapper.securitycheckprogram.SecurityCheckProgramGriddersMapper;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheckprogram.SecurityCheckProgramGridders;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
/**
 * 安全检查项目和网格员关联 Service 实现类
 *
 * @author hans.zhang
 */
@Service
@Validated
public class SecurityCheckProgramGriddersServiceImpl extends ServiceImpl<SecurityCheckProgramGriddersMapper, SecurityCheckProgramGridders> implements SecurityCheckProgramGriddersService {

    @Resource
    private SecurityCheckProgramGriddersMapper securityCheckProgramGriddersMapper;

    @Override
    public Boolean createSecurityCheckProgramGridders(SecurityCheckProgramGriddersCreateReqVO createReqVO) {
        // 插入
        List<Gridders> griddersList = createReqVO.getGridders();
        List<SecurityCheckProgramGridders> securityCheckProgramGriddersList = new ArrayList<>();
        griddersList.forEach(gridders -> {
            SecurityCheckProgramGridders securityCheckProgramGridders = new SecurityCheckProgramGridders();
            securityCheckProgramGridders.setSecurityCheckProgramId(createReqVO.getSecurityCheckProgramId());
            securityCheckProgramGridders.setGriddersId(gridders.getId());
            securityCheckProgramGridders.setGriddersName(gridders.getName());
            securityCheckProgramGriddersList.add(securityCheckProgramGridders);
        });
        // 返回
        securityCheckProgramGriddersMapper.insertBatch(securityCheckProgramGriddersList);
        return Boolean.TRUE;
    }

    @Override
    public List<SecurityCheckProgramGridders> getSecurityCheckProgramGriddersList(Long securityCheckProgramId) {
        LambdaQueryWrapper<SecurityCheckProgramGridders> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecurityCheckProgramGridders::getSecurityCheckProgramId, securityCheckProgramId);
        return securityCheckProgramGriddersMapper.selectList(wrapper);
    }


    @Override
    public Boolean upodateSecurityCheckProgramGridders(SecurityCheckProgramGriddersCreateReqVO reqVO) {
        // 删除旧数据
        LambdaQueryWrapper<SecurityCheckProgramGridders> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecurityCheckProgramGridders::getSecurityCheckProgramId, reqVO.getSecurityCheckProgramId());
        securityCheckProgramGriddersMapper.delete(wrapper);
        // 插入新数据
        List<Gridders> griddersList = reqVO.getGridders();
        List<SecurityCheckProgramGridders> securityCheckProgramGriddersList = new ArrayList<>();
        griddersList.forEach(gridders -> {
            SecurityCheckProgramGridders securityCheckProgramGridders = new SecurityCheckProgramGridders();
            securityCheckProgramGridders.setSecurityCheckProgramId(reqVO.getSecurityCheckProgramId());
            securityCheckProgramGridders.setGriddersId(gridders.getId());
            securityCheckProgramGridders.setGriddersName(gridders.getName());
            securityCheckProgramGriddersList.add(securityCheckProgramGridders);
        });
        // 返回
        securityCheckProgramGriddersMapper.insertBatch(securityCheckProgramGriddersList);
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteByCheckProgramId(Long securityCheckProgramId) {
        LambdaQueryWrapper<SecurityCheckProgramGridders> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecurityCheckProgramGridders::getSecurityCheckProgramId, securityCheckProgramId);
        securityCheckProgramGriddersMapper.delete(wrapper);
        return Boolean.TRUE;
    }

}
